import {
  eventsList,
  eventsOperate,
  eventsDetails,
  ableOperate,
  eventReceiverList,
  eventReceiverOperate,
  eventReceiverDelete,
  selectReleaseRuleList,
} from '@/services/manageMsg';
import { memberExpression } from '@babel/types';
import Prompt from '@/components/Prompt';

export default {
  namespace: 'events',

  state: {
    eventsList: {},
    eventsDetails: {},
    dictDataDetails: {},
    eventReceiverList: [],
    releaseRule: [],
  },

  effects: {
    //一、事件管理：
    *eventsList({ payload, callback }, { call, put }) {
      const response = yield call(eventsList, payload);
      if (response.code === 0) {
        const { list, pageSize, total, pageNum } = response.data;
        yield put({
          type: 'show',
          payload: {
            eventsList: {
              pagination: {
                current: pageNum,
                pageSize,
                total,
              },
              list,
            },
          },
        });
        callback && callback(list);
      }
    },
    *eventsOperate({ payload, callback }, { call }) {
      const response = yield call(eventsOperate, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        Prompt({ content: message });
        if (callback) callback(response.data);
      }
    },
    *eventsDetails({ payload, callback }, { call, put }) {
      const response = yield call(eventsDetails, payload);
      if (response.code === 0) {
        yield put({
          type: 'detail',
          payload: {
            eventsDetails: { [payload.id]: response.data },
          },
        });
        callback && callback(response.data);
      }
    },
    *ableOperate({ payload, callback }, { call }) {
      const response = yield call(ableOperate, payload);
      if (response.code === 0) {
        Prompt({ content: response.message });
        callback(response);
      }
    },
    // 二、事件接收方
    *eventReceiverList({ payload, callback }, { call, put }) {
      const response = yield call(eventReceiverList, payload);
      if (response.code === 0) {
        // const { list, pageSize, total, pageNum } = response.data;
        yield put({
          type: 'ReceiverList',
          payload: {
            // eventReceiverList: {
            //   pagination: {
            //     current: pageNum,
            //     pageSize,
            //     total,
            //   },
            //   list,
            // },
            eventReceiverList: response.data,
          },
        });
        callback && callback(response.data);
      }
    },
    *eventReceiverOperate({ payload, callback }, { call }) {
      const response = yield call(eventReceiverOperate, payload);
      const message = payload.id ? `编辑${response.message}` : `新增${response.message}`;
      if (response.code === 0) {
        Prompt({ content: message });
        if (callback) callback(response.data);
      }
    },
    *eventReceiverDelete({ payload, callback }, { call }) {
      const response = yield call(eventReceiverDelete, payload);
      const { code, message } = response;
      if (code === 0) {
        Prompt({ content: message });
        if (callback) callback(data);
      }
    },
    //规则选择列表
    *selectReleaseRuleList({ payload, callback }, { call }) {
      const response = yield call(selectReleaseRuleList, payload);
      const { code, message } = response;
      if (code === 0) {
        if (callback) callback(response.data);
      }
    },
  },

  reducers: {
    show(state, { payload }) {
      return {
        ...state,
        eventsList: payload.eventsList,
      };
    },
    ReceiverList(state, { payload }) {
      return {
        ...state,
        eventReceiverList: payload.eventReceiverList,
      };
    },
    detail(state, { payload }) {
      return {
        ...state,
        eventsDetails: { ...state.eventsDetails, ...payload.eventsDetails },
      };
    },
  },
};
